package druid;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
import com.alibaba.druid.sql.dialect.hive.visitor.HiveOutputVisitor;
import com.alibaba.druid.sql.dialect.hive.visitor.HiveSchemaStatVisitor;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.sql.parser.SQLParserUtils;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
import com.alibaba.druid.util.JdbcUtils;
import java.util.List;

public class Test2 {

    public static void main(String[] args) {

        String sql = "SELECT a.id,a.name,a.address,a.date,b.math,b.english,b.chinese FROM default.tb_demo065_tel AS b,tb_demo065 AS a WHERE a.id=b.id";
        // String sql="SELECT a.id,a.name,a.address FROM default.tb_demo065_tel a";
        StringBuffer select = new StringBuffer();
        StringBuffer from = new StringBuffer();
        StringBuffer where = new StringBuffer();

        // parser得到AST
        SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcUtils.HIVE);

        SQLStatement sqlStatement = parser.parseStatement();
        List<SQLStatement> stmtList = parser.parseStatementList();
        // 将AST通过visitor输出
        SQLASTOutputVisitor visitor = SQLUtils.createFormatOutputVisitor(from, stmtList, JdbcUtils.HIVE);
        MySqlSchemaStatVisitor d;
        for (SQLStatement stmt : stmtList) {
            stmt.accept(visitor);
            if (stmt instanceof SQLSelectStatement) {
                SQLSelectStatement sstmt = (SQLSelectStatement) stmt;
                SQLSelect sqlselect = sstmt.getSelect();

                SQLSelectQueryBlock query = (SQLSelectQueryBlock) sqlselect.getQuery();

                SQLTableSource ts = query.getFrom();

                if (ts instanceof SQLJoinTableSource) {
                    SQLJoinTableSource s = (SQLJoinTableSource) ts;
                    SQLExprTableSource left = (SQLExprTableSource) s.getLeft();
                    SQLExprTableSource right = (SQLExprTableSource) s.getRight();

                    String leftSQL = left.getExpr().toString();
                    String rightSQL = right.getExpr().toString();

                } else if (ts instanceof SQLExprTableSource) {

                }
                System.out.println("====>" + query.getFrom().findTableSource(1));

                System.out.println("====>" + query.getFrom().findTableSourceWithColumn(1));

                System.out.println("====>" + query.getFrom().findTableSource(2));

                System.out.println("====>" + query.getFrom().findTableSourceWithColumn(2));
                System.out.println("====>" + query.getSelectList().toString());

                HiveSchemaStatVisitorRewriter h = new HiveSchemaStatVisitorRewriter();

                sqlStatement.accept(h);

            }
        }

    }
}
